<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item :label="$t('employee_name')" prop="employeeName">
        <el-input
          v-model="queryParams.employeeName"
          :placeholder="$t('please_enter', { name: $t('employee_name') })"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('employee_code')" prop="employeeCode">
        <el-input
          v-model="queryParams.employeeCode"
          :placeholder="$t('please_enter', { name: $t('employee_code') })"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('employer_name')" prop="employerName">
        <el-input
          v-model="queryParams.employerName"
          :placeholder="$t('please_enter', { name: $t('employer_name') })"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="离职原因" prop="dimissionType">
        <el-select
          v-model="queryParams.dimissionType"
          placeholder="请选择离职原因"
          clearable
        >
          <el-option
            v-for="dict in dict.type.depart_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item :label="$t('processor')" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          :placeholder="$t('please_enter', { name: $t('processor') })"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{ $t("search") }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("reset")
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['employee:employee:add']"
          >{{ $t("add") }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleBatcheDel"
          v-hasPermi="['employee:employee:add']"
          >{{ $t("delete") }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['employee:employee:import']"
          >{{ $t("import") }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['employee:employee:export']"
          >{{ $t("export") }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="default"
          plain
          icon="el-icon-s-promotion"
          size="mini"
          @click="handleSendPwd"
          v-hasPermi="['employee:employee:sendPassword']"
          >{{ $t("Send_password") }}</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="employeeList"
      @selection-change="handleSelectionChange"
      border
      :max-height="tableHeight"
    >
    
    <el-table-column type="selection" width="55" align="center"/>
      <el-table-column
        :label="$t('name')"
        align="center"
        prop="employeeName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('employee_code')"
        align="center"
        prop="employeeCode"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('mobile_number')"
        align="center"
        prop="phone"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('employing_company')"
        align="center"
        prop="employerNames"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <!-- <el-table-column
        label="部门"
        align="center"
        prop="dateOfBirth"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column
        :label="$t('company_email')"
        align="center"
        prop="emailCompany"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        :label="$t('status')"
        align="center"
        prop="employeeStatus"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        :label="$t('processor')"
        align="center"
        prop="createBy"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        :label="$t('action')"
        align="center"
        class-name="small-padding fixed-width"
        width="240"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['employee:employee:view']"
            >{{ $t("view") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['employee:employee:edit']"
            >{{ $t("edit") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-paperclip"
            @click="handleAddPlan(scope.row)"
            v-hasPermi="['employee:employee:plan']"
            >{{ $t("dedicated_contribution_plan") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['employee:employee:remove']"
            >{{ $t("delete") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleSentPassWord(scope.row)"
            v-hasPermi="['employee:employee:sendPassword']"
            >{{ $t("Send_password") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleModifyingHolidays(scope.row)"
            v-hasPermi="['employee:employee:modifyingHolidays']"
            >{{ $t("modifying_holidays") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[10,20,30,50,100, 200]"
    />

    <!-- 添加或修改雇员对话框 -->
    <employeeEdit
      ref="employeeEdit"
      :fieldConfig="fieldConfig"
      @refreshList="getList"
    ></employeeEdit>

    <!-- 导入弹窗 -->
    <importModal
      ref="importModal"
      :title="$t('import_employee')"
      downloadUrl="business/employee/base/downloadTemplate.do"
      uploadUrl="business/employee/base/importTemplate.do"
      :downloadName="$t('download_template')"
      @refreshList="getList"
    ></importModal>

    <!-- 雇员详情框 -->
    <employeeDetail
      :employerOptions="employerOptions"
      :fieldConfig="fieldConfig"
      ref="employeeDetail"
    ></employeeDetail>
    <!-- 雇员供款计划 -->
    <employeePlan :fieldConfig="fieldConfig" ref="employeePlan"></employeePlan>
    <!-- 修改假期 -->
    <modifyingHolidays :fieldConfig="fieldConfig" ref="modifyingHolidays"></modifyingHolidays>
  </div>
</template>

<script>
import { listEmployee, delEmployee,sendPassword } from "@/api/custom/employeeList";
import { listUser, employeefieldconfigInfo } from "@/api/custom/employerList";
import importModal from "@/views/components/custom/importModal.vue";
import employerSelect from "@/views/components/custom/employerSelect.vue";
import employeeEdit from "./edit.vue";
import employeeDetail from "./detail/index.vue";
import employeePlan from "./detail/planList.vue";
import modifyingHolidays from "./modifyingHolidays.vue";

export default {
  name: "Employee",
  dicts: ["depart_type", "sys_user_sex", "job_status"],
  components: {
    importModal,
    employerSelect,
    employeeEdit,
    employeeDetail,
    employeePlan,
    modifyingHolidays
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 雇员表格数据
      employeeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        employeeName: null,
        dimissionType: null,
        createBy: null,
        employeeCode:null,
        employerName:null

      },
      employerOptions: [],
      fieldConfig: {},
    };
  },
  created() {
    this.getList();
    this.getEmployerList();
    this.getFieldConfig();
  },
  methods: {
    handleBatcheDel() {
			if (this.ids.length === 0) return this.$message.warning(this.$t("please_select", { name: this.$t("data_source") }))
			this.handleDelete({})
		},
    handleSendPwd(){
      const ids = this.ids;
      this.$modal
        .confirm(this.$t("Send_password"))
        .then(function () {
          return sendPassword(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("operation_successful"));
        })
        .catch(() => {});
    },
    /** 查询雇员列表 */
    getList() {
      this.loading = true;
      listEmployee(this.queryParams).then((response) => {
        this.employeeList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getFieldConfig() {
      employeefieldconfigInfo().then((res) => {
        this.fieldConfig = res.data ? JSON.parse(res.data.fieldConfig) : {};
        console.log("🐛 ~ employeefieldconfigInfo ~ this.fieldConfig:", this.fieldConfig)
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.employeeEdit.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.employeeEdit.handleUpdate(row);
    },
    /*查看按钮操作 */
    handleView(row) {
      this.$refs.employeeDetail.handleView(row);
    },
    /*供款计划按钮操作 */
    handleAddPlan(row) {
      this.$refs.employeePlan.handlePlan(row);
    },
    // 修改假期
    handleModifyingHolidays(row) {
      this.$refs.modifyingHolidays.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(this.$t("delete_data"))
        .then(function () {
          return delEmployee(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("operation_successful"));
        })
        .catch(() => {});
    },
    /** 发送密码按钮操作 */
    handleSentPassWord(row) {
      const ids = row.id;
      this.$modal
        .confirm(this.$t("Send_password"))
        .then(function () {
          return sendPassword(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("operation_successful"));
        })
        .catch(() => {});
    },
    
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/employee/base/export",
        {
          ...this.queryParams,
        },
        `雇员列表_${new Date().getTime()}.xlsx`
      );
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.importModal.showModal();
    },
    /* 雇主下拉框数据 */
    getEmployerList() {
      listUser({
        pageNum: 1,
        pageSize: 999,
      }).then((res) => {
        this.employerOptions = res.data.records.map((item) => {
          return {
            label: item.userName,
            value: item.id,
            raw: {
              listClass: "",
            },
          };
        });
      });
    },
  },
};
</script>
